package com.atguigu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.atguigu.entity.EduSubject;
import com.atguigu.entity.excel.SubjectData;
import com.atguigu.entity.subject.OneSubject;
import com.atguigu.entity.subject.TwoSubject;
import com.atguigu.listener.SubjectExcelListener;
import com.atguigu.mapper.EduSubjectMapper;
import com.atguigu.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author wxc
 * @since 2021-07-03
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 添加课程分类
     * @param file
     **/
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try{
            //文件输入流
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService))
                    .sheet()
                    .doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取课程的一级二级分类
     **/
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1、查询所有一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", 0);
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapperOne);

        //2、查询所有二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", 0);
        List<EduSubject> twoSubjects = baseMapper.selectList(wrapperTwo);

        //创建list集合，用于存储最终封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //3、封装一级分类
        for (EduSubject eduSubject : oneSubjects) {
            OneSubject oneSubject = new OneSubject();
            //one.setId(oneSubject.getId());
            //one.setTitle(oneSubject.getTitle());

            BeanUtils.copyProperties(eduSubject, oneSubject);

            //在一级分类循环遍历查询所有的二级分类
            List<TwoSubject> twoFinalList = new ArrayList<>();
            //获取每个二级分类
            for (EduSubject subject : twoSubjects) {
                //判断二级分类的parent_id是否和一级分类的id相同
                if(subject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subject, twoSubject);
                    twoFinalList.add(twoSubject);
                }
            }

            //把二级分类的结构放到对应的一级分类的对象中
            oneSubject.setChildren(twoFinalList);

            finalSubjectList.add(oneSubject);
        }

        //4、封装二级分类
        return finalSubjectList;
    }
}
